from django.db import models
from basicweb.models import AreaPlace


# 职位
class Position(models.Model):
    name = models.CharField(verbose_name="姓名", max_length=100, unique=True, blank=False, null=False)
    desc = models.TextField(verbose_name="描述", blank=True, null=True, default=None)
    class Meta:
        managed = True
        app_label = "useweb"
        db_table = "User_Position"

    def __str__(self):
        return "%s" % (self.name)


# 按钮
class Button(models.Model):
    name = models.CharField(verbose_name="名称", max_length=100, unique=True, blank=False, null=False)
    desc = models.TextField(verbose_name="描述", blank=True, null=True, default=None)
    class Meta:
        managed = True
        app_label = "useweb"
        db_table = "User_Button"

    def __str__(self):
        return "%s" % (self.name)


#  Staff 【id, name, Branch(FK), mobile, email,  create_time, edit_time, last_login】
class Staff(models.Model):
    id = models.CharField(verbose_name="工号", max_length=100, primary_key=True, blank=False, null=False)
    pwd = models.CharField(verbose_name="密码", max_length=200, blank=False, null=False)
    name = models.CharField(verbose_name="姓名", max_length=100, blank=False, null=False)
    status = models.BooleanField(verbose_name="状态", default=True)
    position = models.ForeignKey(verbose_name="职位", to=Position, on_delete=models.PROTECT)
    areaplace = models.ManyToManyField(verbose_name="场地", to=AreaPlace,blank=True, null=True, default=None)
    mobile = models.CharField(verbose_name="电话", max_length=100, blank=True, null=True, default=None)
    email = models.CharField(verbose_name="邮箱", max_length=100, blank=True, null=True, default=None)
    create_time = models.DateTimeField(verbose_name="创建时间",blank=True, null=True, default=None)
    edit_time = models.DateTimeField(verbose_name="修改时间", blank=True, null=True, default=None)
    last_login = models.DateTimeField(verbose_name="上次登录", blank=True, null=True, default=None)

    class Meta:
        managed = True
        app_label = "useweb"
        db_table = "User_Staff"

    def __str__(self):
        return "%s" % (self.name)


# 角色
class Roles(models.Model):
    name = models.CharField(verbose_name="姓名", max_length=100, blank=False, null=False)
    staff = models.ManyToManyField(verbose_name="员工", to=Staff, null=True, blank=True)
    desc = models.CharField(verbose_name="描述", max_length=200, null=True, blank=True, default=None)

    class Meta:
        managed = True
        app_label = "useweb"
        db_table = "User_Roles"

    def __str__(self):
        return "%s" % (self.name)



# 权限
class Permission(models.Model):
    title = models.CharField(verbose_name="名称", max_length=100, unique=True, blank=False, null=False)
    url = models.CharField(verbose_name="URL", max_length=100,blank=True, null=True, default=None)
    icon = models.CharField(verbose_name="图标", max_length=100,blank=True, null=True, default=None)
    order = models.IntegerField(verbose_name="排序系数", blank=True, null=True, default=1) # 默认从小到大排序
    parent = models.IntegerField(verbose_name="父节点", blank=True, null=True, default=None)
    button = models.ManyToManyField(verbose_name="按钮", to=Button, blank=True, null=True, default=None)

    class Meta:
        managed = True
        app_label = "useweb"
        db_table = "User_Permission"

    def __str__(self):
        return "%s" % (self.title)


class RolesPermission(models.Model):
    roles = models.ForeignKey(verbose_name="角色", to=Roles, on_delete=models.PROTECT)
    permission = models.ForeignKey(verbose_name="页面权限", to=Permission, on_delete=models.PROTECT)

    class Meta:
        managed = True
        app_label = "useweb"
        db_table = "User_RolesPermission"

    def __str__(self):
        return "%s" % (self.id)


class RolesPermissonDetail(models.Model):
    rolespermission = models.ForeignKey(verbose_name="角色权限", to=RolesPermission, on_delete=models.PROTECT)
    button = models.ForeignKey(verbose_name="按钮",to=Button, on_delete=models.PROTECT)


    class Meta:
        managed = True
        app_label = "useweb"
        db_table = "User_RolesPermissonDetail"

    def __str__(self):
        return "%s" % (self.id)